<?php
	function __construct(){
		require './model/cart_model.php';
		$_GET['a']();
	}
	//---------------------------------------
	function put(){//添加商品
		$_GET['tblname'] = 'shop_goods';
		$_GET['pk'] = 'gid';
		if(empty($_SESSION['cart'][$_GET['gid']])){
			$_SESSION['cart'][$_GET['gid']] = select(" WHERE gid='$_GET[gid]'")[0];
			$_SESSION['cart'][$_GET['gid']]['num'] = 1;
		}else{
			$_SESSION['cart'][$_GET['gid']]['num'] += 1;
		}
		require './view/cart/okcart.php';
	}
	
	function index(){//购物车结算
		require './view/cart/index.php';
	}
	
	function change(){//改变数量
		if(isset($_GET['act']) && ($_GET['act']=='cut')){
			if($_SESSION['cart'][$_GET['gid']]['num']<=0){
				$_SESSION['cart'][$_GET['gid']]['num']=0;
			}else{
				$_SESSION['cart'][$_GET['gid']]['num']-=1;
			}
		}elseif(isset($_GET['act']) && ($_GET['act']=='add')){
			if($_SESSION['cart'][$_GET['gid']]['num']>=$_SESSION['cart'][$_GET['gid']]['cnt']){
				$_SESSION['cart'][$_GET['gid']]['num']=$_SESSION['cart'][$_GET['gid']]['cnt'];
			}else{
				$_SESSION['cart'][$_GET['gid']]['num'] += 1;
			}
		}
		echo '<meta http-equiv="refresh" content="0;./index.php?c=cart&a=index">';
	}
	
	function cut(){//删除单品
		unset($_SESSION['cart'][$_GET['gid']]);
		echo '<meta http-equiv="refresh" content="0;./index.php?c=cart&a=index">';
	}
	
	function truncate(){
		$_SESSION['cart']=[];
		echo '<meta http-equiv="refresh" content="0;./index.php?c=cart&a=index">';
	}
	
?>